﻿using System.Text;
using System.Collections.Generic;
using BrnMall.Manager.Domain.Messages;
using PetaPoco;

namespace BrnMall.Manager.Manager.Messages
{
    public static class MessagesManager
    {
        /// <summary>
        /// 新增创意信息
        /// </summary>
        /// <param name="MsgModel"></param>
        /// <returns></returns>
        public static bool Insert(MessagesModel MsgModel)
        {
            object obj = null;
            using (var db = new QDMallDatabase())
            {
                obj = db.Insert(MsgModel);
                if (obj != null)
                    return true;
                return false;
            }
        }

        /// <summary>
        /// 修改留言信息
        /// </summary>
        /// <param name="MsgModel"></param>
        /// <returns></returns>
        public static bool UpdateMsg(MessagesModel MsgModel)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Update(MsgModel);
            }
            return count > 0 ? true : false;
        }


        public static bool Check(MessagesModel model)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute("update bma_Messages set ischeck=@0,Comment=@1,LinkType=@2,LinkValue=@3 where msgid=@4", model.IsCheck, model.Comment, model.LinkType, model.LinkValue, model.Msgid);
            }
            return count > 0 ? true : false;
        }

        /// <summary>
        /// 删除留言
        /// </summary>
        /// <param name="Msgid"></param>
        /// <returns></returns>
        public static bool DeleteMsg(int Msgid)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute("delete bma_Messages where Msgid=@0", Msgid);
            }
            return count > 0 ? true : false;
        }
       
        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="Msgid"></param>
        /// <param name="IsCheck"></param>
        /// <returns></returns>
        public static MessagesModel GetMsg(int Msgid)
        {
            using (var db = new QDMallDatabase())
            {
                string sql = string.Format("select top 1 * from  bma_Messages where Msgid={0} and isDelete=0", Msgid);
                return db.SingleOrDefault<MessagesModel>(sql, Msgid);
            }

        }
  
        /// <summary>
        /// 获取选项列
        /// </summary>
        /// <param name="Msgid"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<MessagesModel> GetList(int Msgid, int pageSize = 10)
        {
            string sql = string.Format("select top {0} * from bma_Messages where Mesgid={1} and isdelete=0 order by Msgid desc", pageSize, Msgid);
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<MessagesModel>(sql);
            }
        }

        /// <summary>
        /// 获取选项列表（分页模式）
        /// </summary>
        /// <param name="Msgid"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static Page<MessagesModel> GetListPage(int page, int pageSize = 10)
        {
            var sql = new StringBuilder();
            sql.AppendFormat("select * from bma_Messages where isDelete=0  order by msgid desc");
            using (var db = new QDMallDatabase())
            {
                return db.Page<MessagesModel>(page, pageSize, sql.ToString());
            }
        }

        public static Page<MessagesModel> GetListPage(int storeId, int page, int pageSize = 10)
        {
            var sql = new StringBuilder();
            sql.AppendFormat("select * from bma_Messages where storeid={0} and isDelete=0 ", storeId);
            sql.Append(" order by msgid asc");
            using (var db = new QDMallDatabase())
            {
                return db.Page<MessagesModel>(page, pageSize, sql.ToString());
            }
        }

        //public static IList<MessagesModel> GetMessagesList(int storeId)
        //{
        //    using (var db = new QDMallDatabase())
        //    {
        //        return db.Fetch<MessagesModel>("select * from bma_messages where storeId={0} and isDelete=0");
        //    }
        //}
    }
}
